package com.meadin.agent.utils;

import java.util.ArrayList;
import java.util.List;

import com.meadin.agent.domain.BaseMonitor;
import com.meadin.agent.domain.BrandPosterDto;

public class StringUtils {

	public static String getResponse(String str) {
		str = str.replace("'", "");
		str = str.replace("=>", "=");
		str = str.replace("\n", "");
		str = str.replace(" ", "");
		str = str.replace(",", "");
		str = str.replace(")", "");
		str = str.replace("(", "");
		str = str.replace("<a", "");
		str = str.replace("</a>", "");
		str = str.replace("<li>", "");
		String[] split = str.split("</li>");
		for (String string : split) {
			int a = string.indexOf("href=");
			int b = string.indexOf("target");
			int c = string.indexOf(">");
			BrandPosterDto dto = new BrandPosterDto();
			if (b < 0) {
				b = c;
			}
			dto.setHref(string.substring(a + 5, b--));
			dto.setName(string.substring(c + 1, string.length() - 2));
			System.out.println(dto.toString());
		}
		return null;
	}

	public static String getName(String strName) {
		try {
			String[] split = strName.split(" ");
			return split[0];
		} catch (Exception e) {

		}
		return "";
	}

	public static String getAdress(String strAdress) {
		try {
			String[] split = strAdress.split(" ");
			if (split.length > 2) {
				return split[1];
			}

			return strAdress;
		} catch (Exception e) {

		}
		return "";
	}

	public static String getPicUrl(String strAdress) {
		try {
			String[] split = strAdress.split(" ");
			if (split.length > 3) {
				String url = split[3].substring(5, split[3].length() - 7);
				return "http:" + url;
			}
			return strAdress;

		} catch (Exception e) {
		}
		return "";
	}

	public static Integer getPage(String pageStr) {
		String page = "";
		if (pageStr.contains("...")) {
			int dian = pageStr.lastIndexOf("...");
			int hanzi = pageStr.lastIndexOf("跳");
			page = pageStr.substring(dian + 3, hanzi);
		} else if (1 < pageStr.length()) {
			int hanzi = pageStr.lastIndexOf("跳");
			String ye1 = pageStr.substring(0, hanzi);
			page = ye1.substring(ye1.length() - 2, ye1.length() - 1);
		} else {
			return 1;
		}

		return Integer.parseInt(page);
	}

	public static String getSql(String sql) {

		return sql.substring(sql.indexOf("from"));
	}

	public static String getCount(String count) {

		return count.substring(3, count.length() - 3).replaceAll(",", "");
	}

	public static List<BaseMonitor> stringToBaseMonitor(List<String> list) {
		List<BaseMonitor> baseList = new ArrayList<>();
		for (String string : list) {
			String[] split = string.split(",");
			BaseMonitor baseMonitor = new BaseMonitor();
			if (split.length > 1) {
				baseMonitor.setId(split[0]);
				baseMonitor.setName(split[1]);
				baseList.add(baseMonitor);
			}
		}
		return baseList;

	}
}
